(function () {
    const csv = document.querySelector('.csv')
    const json = document.querySelector('.json')
        ; (function () {
            csv.value = localStorage.getItem('csvdata')
        })()
    function toJSON() {
        if (!csv.value) {
            alert('csv值不能为空！')
            json.value = ''
            return
        }
        const data = csv.value.split(/[\n\t\s]+/g)
        const json_data = []
        const head = []
        for (let i = 0; i < data[0].split(',').length; i++) head.push(data[0].split(',')[i])
        if (data.length === 1) {
            const single_json = {}
            for (let i = 0; i < head.length; i++) single_json[head[i]] = ''
            json_data.push(single_json)
        }
        else {
            for (let i = 1; i < data.length; i++) {
                const single_json = {}
                if (data[i].split(',').length != head.length) {
                    alert('csv格式有误！')
                    return
                }
                for (let j = 0; j < data[i].split(',').length; j++) {
                    single_json[head[j]] = data[i].split(',')[j]
                }
                json_data.push(single_json)
            }
        }
        json.innerHTML = JSON.stringify(json_data).replaceAll('},', '},\n')
    }
    $('.convert').on('click', function () {
        toJSON()
    })
    $('.csv').on('mouseleave', function () {
        localStorage.setItem('csvdata', this.value)
    })
    $('.csv_btn .upload').on('click', function () {

    })
    $('.csv_btn .download').on('click', function () {

    })
    $('.json_btn .upload').on('click', function () {

    })
    $('.json_btn .download').on('click', function () {

    })
})()